<?php
/**
 * Created by PhpStorm.
 * UsersF: Mloong
 * Date: 2018/12/3
 * Time: 14:18
 */

namespace app\api\service;


use app\api\model\Mobile;
use app\api\model\Record;
use Driver\Cache\Redis;

class MobileService
{
    # 检查手机号码是否存在
    public static function findMobile($phone)
    {
        $model = new Record();
        $info =  $model->where('phone_num','=',$phone)
            ->find();
        if($info){
            # 如果存在手机号码 则访问字段加一
            $info->setInc('count',1);
        }
        // return exist是因为已存在，不需要再注册
        return $info ? true : false;
    }
    # 向数据库添加一条记录
    public static function createMobileList($phone,$ip,$from)
    {
        $model = new Record();
        $data = array();
        # 检查from是否在redis存在
        $from = self::checkFrom($from);
        # ip转变成地址 并向数据库插入一条数据
        $data['ip'] = $ip;
        $data['address'] = getAddress($ip);
        $data['phone_num'] = $phone;
        $data['from'] = $from;
        $data['add_time'] = now_time();
        $result = $model->create($data);
        return $result ? true : 'mysql create false';
    }
    # 对from进行校验 如果存在redis则返回对应的值，不存在则返回其他
    private static function checkFrom($from)
    {
        $arr = Redis::hget('From');
        $result = array_search($from,$arr);
        if(!$result){
            $from = "其他";
        }
        return $from;
    }
}